// pages/usesr/index.js
import Poster from 'wxa-plugin-canvas/poster/poster';
//引入 发送网络请求的方法
const http = require('../../utils/http.js')
import regeneratorRuntime from '../../lib/runtime/runtime'
import { saveImageToPhotosAlbum,downloadFile,showModal} from '../../utils/asyncwx.js'
Page({
  data: {
    isShowPosterModal: false,//是否展示海报弹窗
    posterImageUrl: "",//海报地址
    tdays:0,//打卡天数
    uid: '',// 用户的openid     
    userInfo:{}, //用户信息对象   
    tname:null,
    tel:null,    
    add2:null,
    add3:null,
    status:null,
    mylabel:'个人信息'

    
  }, 
  onShow: function () {
    wx.hideShareMenu()
    const userInfo=wx.getStorageSync("userInfo")
    const uid=wx.getStorageSync("uid")
    this.setData({
      userInfo,
      uid
    })
    
    if(userInfo.avatarUrl){
      //有授权的用户信息，调取个人信息
      this.get_data()    
     

    }
  },
   
 
  
  
  async get_data() {
   //获取信息    
    const uid=this.data.uid
    if (uid){
      let url = 'Client/getbalance/';
      let data = {
        uid: this.data.uid,
      }
      let res= await http.post(url,data)
       console.log(res)
        if (res.client.name!=null ) {
          //有名字
         wx.showShareMenu() //显示分享
         wx.setStorageSync('status',res.client.status) //用户状态
         let status="通过"
         let mylabel="个人信息"
         if (res.client.status===0){
          status="审核中"
         }
         if (res.client.status===1){
          status="通过"
          wx.setStorageSync('role',1)
         }
         if (res.client.status===2){
           status="未通过"
           mylabel="个人信息，(请更新后重新提交审核)"
         }
         wx.setStorageSync('tname',res.client.name)
         this.setData({         
           tname: res.client.name,
           tdays: res.client.tcount,
           unit: res.client.unit,
           add2:res.client.add2,
           add3:res.client.add3,
           status:status,
           mylabel:mylabel
 
         })
       }
     
     
    
    }

    


  },
  onShareAppMessage(res) {
    //页面分享
    let uid =this.data.uid // 分享产品的Id
    let nickName=this.data.userInfo.nickName
    if (res.from === 'button') {
      // 来自页面内转发按钮
      //console.log(res.target)
     
    }
    return {
      title: '我是'+nickName+'，邀请您来参加村村响大喇叭打卡。',
      path: `pages/index/index?uid=${uid}` // 分享后打开的页面
    }
   
},
    /**
     * 异步生成海报
     */
    async onCreatePoster () {
      let that = this;
      let title="我已打卡"+this.data.tdays+"天。"
      let digest="湖南省广播电视局公共服务处、湖南潇湘之声广播/芒果动听"
      if (that.data.posterImageUrl !== "") {
        that.setData({
          isShowPosterModal: true
        })
        return;
      }
      let posterConfig = {
        width: 750,
        height: 1200,
        backgroundColor: '#fff',
        debug: false
      }
      var blocks = [
        {
          width: 690,
          height: 808,
          x: 30,
          y: 120,
          borderWidth: 2,
          borderColor: '#f0c2a0',
          borderRadius: 20,
        },
        {
          width: 634,
          height: 74,
          x: 59,
          y: 680,
          backgroundColor: '#fff',
          opacity: 0.5,
          zIndex: 100,
        }
      ]
      var texts = [];
      texts = [
        {
          x: 113,
          y: 61,
          baseLine: 'middle',
          text: that.data.userInfo.nickName+',正在参加村村响大喇叭打卡活动',
          fontSize: 32,
          color: '#080808',
          width: 570,
          lineNum: 1
        },
        // {
        //   x: 32,
        //   y: 113,
        //   baseLine: 'top',
        //   text: '正在参加村村响大喇叭打卡活动',
        //   fontSize: 38,
        //   color: '#080808',
        // },
        {
          x: 59,
          y: 770,
          baseLine: 'middle',
          text: title, //标题
          fontSize: 38,
          color: '#080808',
          marginLeft: 30,
          width: 570,
          lineNum: 2,
          lineHeight: 50
        },
        {
          x: 59,
          y: 845,
          baseLine: 'middle',
          text: digest, //digest
          fontSize: 32,
          color: '#929292',
          width: 560,
          lineNum: 2,
          lineHeight: 50
        },
        {
          x: 315,
          y: 1100,
          baseLine: 'top',
          text: '长按识别小程序码,立即参加',
          fontSize: 28,
          color: '#929292',
        }
      ];
  
      let imageUrl = "http://buck-cct.oss-cn-shenzhen.aliyuncs.com/lucky/POSTERMAIN.jpg" //海报主图
      imageUrl = imageUrl.replace('http://', 'https://')
      let url = 'Qrcode/GetQrcode/';
      let data = {
        uid: this.data.uid,
      }
      let res= await http.post(url,data)     //从后台获取小程序二维码
      let qrCodeUrl=res.imgurl
     
      console.info(qrCodeUrl)
      var images = [
        {
          width: 65,
          height: 65,
          x: 32,
          y: 30,
          borderRadius: 65,
          url: that.data.userInfo.avatarUrl, //用户头像
        },
        {
          width: 634,
          height: 570,
          x: 59,
          y: 130,
          url: imageUrl,//海报主图
        },
        {
          width: 220,
          height: 220,
          x: 70,
          y: 1000,
          url: qrCodeUrl,//二维码的图
        }
      ];
  
      posterConfig.blocks = blocks;//海报内图片的外框
      posterConfig.texts = texts; //海报的文字
      posterConfig.images = images;
  
      that.setData({ posterConfig: posterConfig }, () => {
        Poster.create(true);    //生成海报图片
      });
  
    },
    /**
   * 生成海报成功-回调
   * @param {} e 
   */
    onPosterSuccess(e) {
      const { detail } = e;
      this.setData({
        posterImageUrl: detail,
        isShowPosterModal: true
        })
    },
    /**
       * 生成海报失败-回调
       * @param {*} err 
       */
      onPosterFail(err) {
        console.info(err)
      },
      /**
   * 点击放大图片
   * @param {} e 
   */
  posterImageClick: function (e) {
    wx.previewImage({
      urls: [this.data.posterImageUrl],
    });
  },
  /**
   * 隐藏海报弹窗
   * @param {*} e 
   */
  hideModal(e) {
    this.setData({
      isShowPosterModal: false
    })
  },
  
 /**
  * 保存海报图片
  */
 async savePosterImage() {
    let res= await saveImageToPhotosAlbum(this.data.posterImageUrl).catch(err=>{
        
        
    })
    //console.log(res0) //errMsg: "saveImageToPhotosAlbum:ok"
    if (res.errMsg === "saveImageToPhotosAlbum:ok") {
    //保存成功
    await showModal({content:'海报已存入手机相册，赶快分享到朋友圈吧!',showCancel:false})
    
    }
    if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
      console.log("当初用户拒绝，再次发起授权")
      let res= await showModal({content:'请授权保存图片!',showCancel:true})
      console.log(res)
      if (res.confirm) {
        wx.openSetting()
      }
    }  
  },
})